package com.arvin.controller;

import com.arvin.pojo.Log;
import com.arvin.pojo.Result;
import com.arvin.pojo.Time;
import com.arvin.service.AdminService;
import com.arvin.service.LogService;
import com.arvin.service.TimeService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@Tag(name = "时间相关接口", description = "时间相关接口")
@Slf4j
public class TimeController {

    @Autowired
    private TimeService timeService;
    @Autowired
    private LogService logService;

    /**
     * 设置考试时间
     *
     * @param time
     * @return
     */
    @Operation(summary = "设置考试时间", description = "设置考试时间")
    @PostMapping("/setExamTime")
    public Result setExamTime(@RequestBody Time time) {
        log.info("设置考试时间:{}", time);
        timeService.setExamTime(time);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("时间");
        log.setLogOperate("管理员" + username + "发布了预约考试信息："
                + "，考试开始日期：" + time.getExamStartDate() + "，考试结束日期：" + time.getExamEndDate()
                + "，考试每日开始时间：" + time.getExamStartTime() + "，考试每日结束结束：" + time.getExamEndTime());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 更新时间
     *
     * @param time
     * @return
     */
    @Operation(summary = "更新时间", description = "更新考试时间")
    @PutMapping("/updateTime")
    public Result updateExamTime(@RequestBody Time time) {
        log.info("更新时间:{}", time);
        timeService.updateTime(time);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("时间");
        log.setLogOperate("管理员" + username + "更新了时间信息：" + "卓越班班级名称：" + time.getClassName()
                + "，考试开始日期：" + time.getExamStartDate() + "，考试结束日期：" + time.getExamEndDate()
                + "，考试每日开始时间：" + time.getExamStartTime() + "，考试每日结束结束：" + time.getExamEndTime()
                + "，面试开始日期：" + time.getIntvStartDate() + "，面试结束日期：" + time.getIntvEndDate()
                + "，面试每日开始时间：" + time.getIntvStartTime() + "，面试每日结束结束：" + time.getIntvEndTime()
                + "，免试开始日期：" + time.getExemStartDate() + "，免试结束日期：" + time.getExemEndDate()
                + "，免试每日开始时间：" + time.getExemStartTime() + "，免试每日结束结束：" + time.getExemEndTime());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 设置面试时间
     *
     * @param time
     * @return
     */
    @Operation(summary = "设置面试时间", description = "设置面试时间")
    @PostMapping("/setIntvTime")
    public Result setIntvTime(@RequestBody Time time) {
        log.info("设置面试时间:{}", time);
        timeService.setIntvTime(time);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("时间");
        log.setLogOperate("管理员" + username + "发布了预约面试信息："
                + "面试开始日期：" + time.getIntvStartDate() + "，面试结束日期：" + time.getIntvEndDate()
                + "，面试每日开始时间：" + time.getIntvStartTime() + "，面试每日结束结束：" + time.getIntvEndTime());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 设置免试时间
     *
     * @param time
     * @return
     */
    @Operation(summary = "设置免试时间", description = "设置免试时间")
    @PostMapping("/setExemTime")
    public Result setExemTime(@RequestBody Time time) {
        log.info("设置免试时间:{}", time);
        timeService.setExemTime(time);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("时间");
        log.setLogOperate("管理员" + username + "发布了预约免试信息："
                + "，免试开始日期：" + time.getExemStartDate() + "，免试结束日期：" + time.getExemEndDate()
                + "，免试每日开始时间：" + time.getExemStartTime() + "，免试每日结束结束：" + time.getExemEndTime());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 查询时间
     *
     * @return
     */
    @Operation(summary = "查询时间", description = "查询时间")
    @GetMapping("/getTime")
    public Result<Time> getTime() {
        log.info("获取时间");
        Time time = timeService.getTime();
        return Result.success(time);
    }
}
